package com.liu.share.util.email;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class MailSenderUtil {
    private final transient Properties props = System.getProperties();
    private transient MailAuthenticator authenticator;
    private transient Session session;

    public static MailSenderUtil getDefaultSender() {
        return MailSenderHolder.DAFY_MAIL_SENDER;
    }

    public MailSenderUtil(String smtpHostName, String username, String password) {
        this.init(username, password, smtpHostName);
    }

    public MailSenderUtil(String username, String password) {
        String smtpHostName = "smtp." + username.split("@")[1];
        this.init(username, password, smtpHostName);
    }

    private void init(String username, String password, String smtpHostName) {
        this.props.put("mail.smtp.auth", "true");
        this.props.put("mail.smtp.host", smtpHostName);
        this.authenticator = new MailAuthenticator(username, password);
        this.session = Session.getInstance(this.props, this.authenticator);
    }

    public void sendFile(List<String> recipients, String subject, Object content,String fileName,String filePath) throws AddressException, MessagingException {
        MimeMessage message = new MimeMessage(this.session);
        message.setFrom(new InternetAddress(this.authenticator.getUsername()));
        int num = recipients.size();
        InternetAddress[] addresses = new InternetAddress[num];

        for(int i = 0; i < num; ++i) {
            addresses[i] = new InternetAddress(recipients.get(i));
        }

        message.setRecipients(RecipientType.TO, addresses);
        message.setSubject(subject);

        //设置邮件消息内容、包含附件
        Multipart msgPart = new MimeMultipart();
        //正文
        MimeBodyPart body = new MimeBodyPart();
        //附件
        MimeBodyPart attach = new MimeBodyPart();

        //设置正文内容
        body.setContent(content, "text/html;charset=utf-8");
        //设置附件内容
        attach.setDataHandler(new DataHandler(new FileDataSource(filePath)));
        attach.setFileName(fileName);

        msgPart.addBodyPart(body);
        msgPart.addBodyPart(attach);

        message.setContent(msgPart);
        message.saveChanges();
        Transport.send(message);
    }


    public void sendContent(List<String> recipients, String subject, Object content) throws MessagingException {
        MimeMessage message = new MimeMessage(this.session);
        message.setFrom(new InternetAddress(this.authenticator.getUsername()));
        int num = recipients.size();
        InternetAddress[] addresses = new InternetAddress[num];

        for(int i = 0; i < num; ++i) {
            addresses[i] = new InternetAddress(recipients.get(i));
        }

        message.setRecipients(RecipientType.TO, addresses);
        message.setSubject(subject);
        message.setContent(content.toString(), "text/html;charset=utf-8");
        Transport.send(message);
    }

    private static class MailSenderHolder {
        public static final MailSenderUtil DAFY_MAIL_SENDER = new MailSenderUtil("smtp.sina.com", "dafypub@sina.com", "nb33@dafy");

        private MailSenderHolder() {
        }
    }

    public static void main(String[] args) {
        List<String> recept = new ArrayList();
        recept.add("weitao0912@163.com");

        try {
            (new MailSenderUtil("smtp.qiye.163.com", "public@dafy.com", "@1024Dafy")).sendContent(recept, "哈哈哈哈", "大是放达是");
        } catch (MessagingException var3) {
            var3.printStackTrace();
        }

    }
}
